const Acceleration = require("Acceleration");
const Video = require("Video");

cc.Class({
    extends: cc.Component,
    properties: {
        spriteBgs: {
            default: [],
            type: [cc.Node]
        },
        spriteClouds: {
            default: [],
            type: [cc.Node]
        },
        bgWidth: 0,
        bgHeight: 0,
        bgY: 0,
        cloudWidth: 0,
        cloudHeight: 0,
        ac: {
            default: null,
            type: Acceleration
        },
        spriteAll: {
            default: [],
            type: [cc.Node]
        },
        lableX: {
            default: null,
            type: cc.Label
        },
        lableY: {
            default: null,
            type: cc.Label
        },
    },

    // use this for initialization
    onLoad: function () {
        this.ac = new Acceleration();
        this.ac.addListener(this.accelerationListener, this);

        this.spriteBgs[0].another = this.spriteBgs[1];
        this.spriteBgs[1].another = this.spriteBgs[0];
        this.spriteClouds[0].another = this.spriteClouds[1];
        this.spriteClouds[1].another = this.spriteClouds[0];
        cc.find('Canvas/main/bg').setLocalZOrder(-20);
        cc.find('Canvas/main/cloud').setLocalZOrder(-10);
        cc.find('Canvas/end').active = false;

        var bgsize = this.spriteBgs[0].getContentSize();
        this.bgWidth = bgsize.width;
        this.bgHeight = bgsize.height;

        var cloudsize = this.spriteClouds[0].getContentSize();
        this.cloudWidth = cloudsize.width;
        this.cloudHeight = cloudsize.height;

        this.scheduleOnce(this.hideTips, 5);
        this.scheduleOnce(this.sceneEnd, 20);//20
        this.scheduleOnce(this.playStarVideo, 20 + 6.5);
    },


    update: function (dt) {
        this.updateBgRotate(dt);
        this.updateCloudMove(dt);
        this.updateBg(dt);
        this.updateCloud(dt);
    },

    updateBg: function (dt) {
        var amend = 100;
        for (var i = 0, len = this.spriteBgs.length; i < len; i++) {
            var spr = this.spriteBgs[i];
            if (spr.getPositionX() > (this.bgWidth + amend)) {
                spr.setPositionX(spr.another.getPositionX() - this.bgWidth);
                this.hideTips();
            } else if (spr.getPositionX() < (-this.bgWidth - amend)) {
                spr.setPositionX(spr.another.getPositionX() + this.bgWidth);
                this.hideTips();
            }
        }
    },

    updateCloud: function (dt) {
        var amend = 100;
        for (var i = 0, len = this.spriteClouds.length; i < len; i++) {
            var spr = this.spriteClouds[i];
            if (spr.getPositionX() > (this.cloudWidth + amend)) {
                spr.setPositionX(spr.another.getPositionX() - this.bgWidth);
            } else if (spr.getPositionX() < (-this.cloudWidth - amend)) {
                spr.setPositionX(spr.another.getPositionX() + this.bgWidth);
            }
        }
    },

    updateCloudMove: function (dt) {
        var moveSpeed = 60;
        var dis = moveSpeed * dt;
        for (var i = 0, len = this.spriteClouds.length; i < len; i++) {
            var spr = this.spriteClouds[i];
            spr.setPositionX(spr.getPositionX() + dis)
        }
    },

    updateBgRotate: function (dt) {
        var rotateSpeed = -4;
        var angle = rotateSpeed * dt;
        for (var i = 0, len = this.spriteBgs.length; i < len; i++) {
            var spr = this.spriteBgs[i];
            spr.rotation = spr.rotation + angle;
        }
    },

    accelerationListener: function (ax, ay, az) {
        if (this.lableX) this.lableX.string = ax + "";
        if (this.lableY) this.lableY.string = ay + "";

        var offx = ax / 10;
        var offy = ay / 20;
        var maxy = 100;
        this.bgY = Math.max(-maxy, Math.min(maxy, this.bgY + offy));

        var sprall = this.spriteAll;
        for (var i = 0, len = sprall.length; i < len; i++) {
            var spr = sprall[i];
            spr.setPositionX(spr.getPositionX() + offx);
            spr.setPositionY(this.bgY);
        }
    },

    hideTips: function () {
        cc.find('Canvas/main/lable/shakehand').active = false;
    },

    sceneEnd: function () {
        this.ac.removeListener();
        var anim = cc.find('Canvas').getComponent(cc.Animation);
        anim.play('starend');
    },

    playStarVideo: function () {
        var v = new Video("resources/imgs/star_poster.png", "resources/videos/star.mp4", this.loadBallScene);
        cc.director.preloadScene("ballScene");
    },

    loadBallScene: function (params) {
        cc.director.loadScene('ballScene');
    }
});
